﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using System.Web.Script.Serialization;
using CommonUtility;
using System.Data.SqlClient;

namespace BLL
{
    public class bllRole
    {
        /// <summary>
        /// 分页获取角色信息
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="roleDesc"></param>
        /// <param name="_sortOrder"></param>
        /// <param name="_pageIndex"></param>
        /// <param name="_pageSize"></param>
        /// <param name="_operator"></param>
        /// <returns></returns>
        public string GetAllRole(string roleID, string roleDesc, string _sortOrder, int _pageIndex, int _pageSize, UserInfo _operator)
        {
            string _where = "1=1";
            string result = "";
            string message = string.Empty;
            int total = 0;
            if (!string.IsNullOrEmpty(roleID))
            {
                _where = _where + " and roleID like '%" + roleID + "%'";
            }
            if (!string.IsNullOrEmpty(roleDesc))
            {
                _where = _where + " and roleDesc like '%" + roleDesc + "%'";
            }
            try
            {
                IList<RoleInfo> list = new List<RoleInfo>();
                list = Role.GetPageObject(_pageIndex, _pageSize, _where, _sortOrder.Trim(), ref total, ref message);
                string json = new JavaScriptSerializer().Serialize(list);
                result = @"{""Rows"":" + json + @",""Total"":""" + total + @"""}";
            }
            catch (Exception ex)
            {
                result = ex.Message;
                Common.Application_Error_Server("Bll/bllRole.cs/GetAllRole(" + _where + ") " + _operator.Name, ex);
            }
            return result;
        }


        public string GetAllRole(string _where, UserInfo _operator)
        {
            string result = "";
            try
            {
                IList<RoleInfo> list = new List<RoleInfo>();
                list = Role.GetList(_where);
                string json = new JavaScriptSerializer().Serialize(list.OrderByDescending(d => d.RID).ToList());
                result = @"{""Rows"":" + json + @",""Total"":""" + list.Count + @"""}";
            }
            catch (Exception ex)
            {
                result = ex.Message;
                Common.Application_Error_Server("BLL/bllRole.cs/GetAllRole(" + _where + ") " + _operator.Name, ex);
            }
            return result;
        }

        /// <summary>
        /// 按条件查询记录
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="_rlName"></param>
        /// <returns></returns>
        public string GetAllRole(string roleID, string roleDesc, UserInfo _operator)
        {
            string result = "";
            string _where = "1=1";
            IList<SqlParameter> paras = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(roleID))
            {
                _where = _where + " and roleID like '%' + @roleID + '%'";
                paras.Add(new SqlParameter("@roleID", roleID));
            }
            if (!string.IsNullOrEmpty(roleDesc))
            {
                _where = _where + " and roleDesc like '%' + @roleDesc + '%'";
                paras.Add(new SqlParameter("@roleDesc", roleDesc));
            }
            try
            {
                IList<RoleInfo> list = new List<RoleInfo>();
                list = Role.GetList(_where, paras.ToArray());
                string json = new JavaScriptSerializer().Serialize(list.OrderByDescending(d => d.RID).ToList());
                result = @"{""Rows"":" + json + @",""Total"":""" + list.Count + @"""}";
            }
            catch (Exception ex)
            {
                result = ex.Message;
                Common.Application_Error_Server("BLL/bllRole.cs/GetAllRole(" + _where + ") " + _operator.Name, ex);
            }
            return result;
        }
        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <param name="_condition"></param>
        /// <param name="_operator"></param>
        /// <returns></returns>
        public IList<RoleInfo> GetList(string _condition, UserInfo _operator)
        {
            string result = string.Empty;
            IList<RoleInfo> list = new List<RoleInfo>();
            try
            {
                list = Role.GetList(_condition);
            }
            catch (Exception ex)
            {
                result = ex.Message;
                Common.Application_Error_Server("BLL/bllRole.cs/GetList(" + _condition + ") " + _operator.Name, ex);
            }
            return list;
        }

        /// <summary>
        /// 新增或保存
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        public string Save(string _funID, string rid, string roleID, string roleDesc, UserInfo _operator)
        {
            string result = string.Empty;
            System.Data.SqlClient.SqlTransaction myTran = SQLCommon.GetSqlTransaction();
            System.Data.SqlClient.SqlConnection myConn = myTran.Connection;
            try
            {
                Model.RoleInfo model = new RoleInfo();

                if (string.IsNullOrEmpty(rid))
                {
                    if (DAL.Role.Count(" roleID='" + roleID + "'") > 0)
                    {
                        return "existed";
                    }
                    else
                    {
                        model.RoleID = roleID;
                        model.RoleDesc = roleDesc;
                        model.Whom = _operator.UserID;
                        model.Flag = "A";
                        DAL.Role.Add(model, myTran);
                        BLL.bllLog.AddLog(_funID, "Add", model.RoleID, _operator.UserID, myTran);
                    }
                }
                else
                {
                    model.RID = Common.ToInt32(rid);
                    model.RoleID = roleID;
                    model.RoleDesc = roleDesc;
                    model.Whom = _operator.UserID;
                    DAL.Role.Edit(model, myTran);
                    BLL.bllLog.AddLog(_funID, "Edit", model.RoleID, _operator.UserID, myTran);
                }


                myTran.Commit();
            }
            catch (Exception ex)
            {
                result = ex.Message;
                myTran.Rollback();
                Common.Application_Error_Server("BLL/bllRole.cs/Save()" + _operator.Name, ex);
            }
            finally
            {
                myConn.Close();
                myTran.Dispose();
                myConn.Dispose();
            }
            return result;
        }


        /// <summary>
        /// 设置是否有效
        /// </summary>
        /// <param name="_funID"></param>
        /// <param name="_rids"></param>
        /// <param name="flag"></param>
        /// <param name="_operator"></param>
        /// <returns></returns>
        public string Process(string _funID, string _rids, string flag, UserInfo _operator)
        {
            string result = string.Empty;
            System.Data.SqlClient.SqlTransaction myTran = SQLCommon.GetSqlTransaction();
            System.Data.SqlClient.SqlConnection myConn = myTran.Connection;
            try
            {
                IList<RoleInfo> list = Role.GetList(" Rid in (" + _rids + ")");
                var desc = string.Join(",", list.Select(i => i.RoleID).ToList().ToArray());
                foreach (var Roleinfo in list)
                {
                    Roleinfo.Flag = flag;
                    Roleinfo.Whom = _operator.UserID;
                    Role.Edit(Roleinfo, myTran);
                }
                //添加操作记录
                var action = flag == "A" ? "Active" : "Inactive";
                bllLog.AddLog(_funID, action, desc, _operator.UserID, myTran);

                myTran.Commit();
            }
            catch (Exception ex)
            {
                result = ex.Message;
                myTran.Rollback();
                Common.Application_Error_Server("BLL/bllRole.cs/Process(" + _rids + ") " + _operator.Name, ex);
            }
            finally
            {
                myConn.Close();
                myTran.Dispose();
                myConn.Dispose();
            }

            return result;

        }

        public RoleInfo getModifyByRID(int _rID)
        {
            if (_rID == 0) return null;
            return Role.GetModelByID(_rID);
        }
    }
}
